<?php
// $Id: CheckboxInput.php 27 2010-11-16 12:49:54Z mjohnson60@gmail.com $


/**
 * Enter description here ...
 * 
 * @author markjohnson
 * @version 1.0
 * @copyright Copyright (c) 2010, markjohnson
 * @license http://www.gnu.org/licenses/gpl.html The GNU General Public License v3.0
 *
 */
class CheckboxInput extends FormElement
{
	/**
	 * Enter description here ...
	 * 
	 * @var unknown_type
	 */
	var $checked = FALSE;

	/**
	 * Enter description here ...
	 * 
	 * @param string $name
	 * @param string $value
	 * @param string $id
	 * @param FormElement $parent
	 * @param array $attributes
	 * @param string $title
	 * @param bool $checked
	 */
	public function __construct($name, $value, $id = NULL, $parent = NULL, $attributes = array(), $title = '', $checked = FALSE)
	{
		$this->type = 'checkbox';
		$this->title = $title;
		$this->checked = $checked;
		
		if (is_array($value))
		{
			parent::__construct($value, $name);
		}
		else
		{
			parent::__construct(NULL, $name, $value, $id, $parent, $attributes);
		}
	}

	/* 
	 * @see FormElement::_compile()
	 */
	public function _compile()
	{
		return $this->render();
	}

	/* 
	 * @see FormElement::render()
	 */
	public function render()
	{
		$this->addHtmlClasses(array(
			'form-checkbox'
		));
		$output = '<input type="checkbox" id="' . $this->id . '" name="' . $this->name . '" value="' . $this->value . '" ' . ($this->checked === TRUE ? 'checked="checked"' : '') . (empty($this->attributes) ? '' : attributes($this->attributes)) . " />\n";
		
		if (!empty($this->title))
		{
			$output .= '<label class="option" for="' . $this->id . '">' . $this->title . "</label>\n";
			unset($this->title);
		}
		
		return parent::render($output);
	}
}